package com.wyl.clinic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.wyl.clinic.entity.Department;
import com.wyl.clinic.entity.Medicine;
import com.wyl.clinic.entity.MedicineStock;
import com.wyl.clinic.service.MedicineService;
import com.wyl.clinic.service.MedicineStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 药品主信息 前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Autowired
    private MedicineService medicineService;
    @Autowired
    private MedicineStockService medicineStockService;

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<Medicine> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Medicine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Medicine::getName,name);
        }


        IPage<Medicine> result = medicineService.page(page,lambdaQueryWrapper);

        return Result.suc(result.getRecords(),result.getTotal());
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Medicine medicine){
        return medicineService.saveOrUpdate(medicine)?Result.suc():Result.fail();
    }

    @PostMapping("/stockSaveOrUpdate")
    public Result stockSaveOrUpdate(@RequestBody MedicineStock medicineStock){
        return medicineStockService.saveOrUpdate(medicineStock)?Result.suc():Result.fail();
    }

    @GetMapping("/list")
    public Result list(@RequestParam String name){
        LambdaQueryWrapper<Medicine> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Medicine::getName, name);
        return Result.suc(medicineService.list(wrapper));
    }
    @GetMapping("/stockList")
    public Result stockList(@RequestParam Integer medicineId){
        LambdaQueryWrapper<MedicineStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MedicineStock::getMedicineId, medicineId);
        return Result.suc(medicineStockService.list(wrapper));
    }

    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return medicineService.removeById(id)?Result.suc():Result.fail();
    }
    @GetMapping("/stockDel")
    public Result stockDel(@RequestParam Integer id){
        return medicineStockService.removeById(id)?Result.suc():Result.fail();
    }
}
